*I forgot to grab the expected number of chambers the Democrats will attain for this version of the forecast.  

clear
version 15.1
set varabbrev off, permanently
cd C:\Users\User\Dropbox\08_FORECAST\005runs\run20181030

*First get current control before the 2018 elections ready to merge.
import excel 125currentpartycontrol20181101.xlsx, firstrow
save temp, replace

*Get state population
clear
import delimited 126_2017pop20181101.csv
egen sum=sum(pop2017)
replace pop2017=pop2017/sum
*divide it by 2 to take into account the fact there is a house and senate in each state (almost).  SINCE I'M COMPUTING POPULATION FOR STATE SENATES AND HOUSES SEPARATELY, I TOOK THIS OUT.  
*replace pop2017=pop2017/2
drop sum v2
save temp2, replace

use 106simUOACham20181030
merge m:1 sid sen using temp
erase temp.dta
assert _merge!=1
drop if _merge==2
drop _merge
merge m:1 sid using temp2
erase temp2.dta
assert _merge!=1
drop if _merge==2
drop _merge

gen rtod=currentdemcontrol==0&dcont==1
gen dtor=currentdemcontrol==1&dcont==0
gen rtosplit=currentdemcontrol==0&tcont==1
gen dtosplit=currentdemcontrol==1&tcont==1
*Only count the move to split as have a flip.
*recode rtosplit dtosplit (1=.5)
*I TOOK THE ABOVE OUT BECAUSE IT IS TOO HARD TO EXPLAIN.  
gen net=rtod+rtosplit-dtor-dtosplit
foreach string in rtod dtor rtosplit dtosplit net {
gen `string'pop=`string'*pop2017
}
save temp, replace

use temp
collapse (sum) rtod dtor rtosplit dtosplit net rtodpop dtorpop rtosplitpop dtosplitpop netpop, by(iteration)
gen c=1
collapse (median) rtod dtor rtosplit dtosplit net rtodpop dtorpop rtosplitpop dtosplitpop netpop, by(c)
drop c
export delimited 128nationforecast20181101.csv, replace
clear


use temp
collapse (sum) rtod dtor rtosplit dtosplit net rtodpop dtorpop rtosplitpop dtosplitpop netpop, by(iteration sen)
collapse (median) rtod dtor rtosplit dtosplit net rtodpop dtorpop rtosplitpop dtosplitpop netpop, by(sen)
export delimited 129nationforecastbychamber20181101.csv, replace
clear



